import { routing } from "../../utils/routing";

// pages/register/register.ts
Page({
  redirectURL: "",
  data: {
    licNo: "",
    name: "",
    genderIndex: 0,
    genders: ["未知", "男", "女", "其他"],
    birthDate: "1990-01-01",
    licImgURL: "",
    state: "UNSUBMITTED" as "UNSUBMITTED" | "PENDING" | "VERIFIED",
  },
  onLoad(opt: Record<"redirect", string>) {
    const o: routing.registerOpts = opt;
    if (o.redirect) {
      this.redirectURL = decodeURIComponent(o.redirect);
    }
  },
  onUploadlic() {
    wx.chooseMedia({
      success: (res) => {
        console.log(res);
        if (res.tempFiles.length > 0) {
          console.log(res.tempFiles[0].tempFilePath);
          this.setData({
            licImgURL: res.tempFiles[0].tempFilePath,
          });
          //TODO: upload image
          setTimeout(() => {
            this.setData({
              licNo: "123423423",
              name: "张三",
              genderIndex: 1,
              birthDate: "1995-11-4",
            });
          }, 1000);
        }
      },
    });
  },
  onGenderChange(e: any) {
    this.setData({
      genderIndex: e.detail.value,
    });
  },
  onBirthDateChange(e: any) {
    this.setData({
      birthDate: e.detail.value,
    });
  },
  //提交审核
  onSubmit() {
    this.setData({
      state: "PENDING",
    });
    setTimeout(() => {
      this.onLicVerified();
    }, 3000);
  },
  onResubmit() {
    this.setData({
      state: "UNSUBMITTED",
      licImgURL: "",
    });
  },
  onLicVerified() {
    this.setData({
      state: "VERIFIED",
    });
    if (this.redirectURL) {
      wx.redirectTo({
        url: "/pages/lock/lock",
      });
    }
  },
});
